﻿using commonFramwork.LogHelp;
using commonFramwork.Model;
using EF.Framework.Entity;
using Myu.BLL.Base;
using Myu.Interface.InterFace;
using Myu.Redis.Init;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace Myu.BLL.Service
{
    public class AccountService :IAccountService
    {


        public IBaseServices<Users> _usersService = new UnityContainerHelp().GetServer<IBaseServices<Users>>();

        
        public bool CheckAuth(string UserName, string PassWord)
        {
            Users users = new Users() { Password = "sss", UserName = "user" };
            _usersService.Add(users);
            _usersService.SaverChanges();
            return true;
        }

        public List<Users> GetUserList(PageDto page)
        {
             var result =   RedisHelper.RedisHelper.GetCache<List<Users>>($"GetUserList");
            if (result != null)
            {
                return result;
            }
            else
            {
                var list = _usersService.QueryWhere(x => x.Id != 0);
                RedisHelper.RedisHelper.SaveCache<List<Users>>("GetUserList", list);
                return list;
            }
        }


        public bool DeleteUser(int? id)
        {
            var entity = _usersService.QueryWhere(x => x.Id == id)[0];
            try
            {
                _usersService.Delete(entity, false);
                var result = _usersService.SaverChanges();
                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
            
        }


        public  int SaveUserInfo(Users user)
        {
            int id;
            if (user.Id == 0)
            {
                _usersService.Add(user);
                id = _usersService.SaverChanges();
            }
            else
            {
                _usersService.Edit(user);
                id = _usersService.SaverChanges();
            }
            return id;
        }
    }
}
